package com.springboot.springbootlogindemo.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.springbootlogindemo.DTO.UserDTO;
import com.springboot.springbootlogindemo.domain.Follow;
import com.springboot.springbootlogindemo.domain.User;
import com.springboot.springbootlogindemo.domain.UserInfo;
import com.springboot.springbootlogindemo.mapper.FollowMapper;
import com.springboot.springbootlogindemo.service.FollowService;
import com.springboot.springbootlogindemo.utils.Result;
import com.springboot.springbootlogindemo.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow>  implements FollowService {

    @Resource
    private FollowMapper followMapper;



    /**
     * 实现关注功能
     * @param followId
     * @return
     */
    @Override
    public Result follow(Long followId) {
        Follow follow =new Follow();
        follow.setId(UserHolder.getUser().getId());
        follow.setFollowUserId(followId);
        followMapper.insert(follow);
        return Result.success("关注成功");
    }

    /**
     * 获取关注的人的部分信息
     * @return
     */
    @Override
    public Result showAll() {
        List<User> userList = followMapper.findFollowedUsersByUserId(UserHolder.getUser().getId());
        List<UserDTO> userDTOList=new ArrayList<>();
        for(User user:userList){
            UserDTO userDTO = BeanUtil.copyProperties(user,UserDTO.class);
            userDTOList.add(userDTO);
        }
        return Result.success(userDTOList,"获取成功");
    }

    /**
     * 获取某个关注的人的全部可展示信息
     * @param followId
     * @return
     */
    @Override
    public Result showOne(Long followId) {
        UserDTO userDTO = followMapper.findUserAndUserInfoByFollowId(followId);
        return Result.success(userDTO,"获取成功");

    }
}
